//秒转分秒
export function s_to_hs(s) {
  var h
  h = Math.floor(s / 60)
  s = s % 60
  h += ''
  s += ''
  h = h.length == 1 ? '0' + h : h
  s = s.length == 1 ? '0' + s : s
  if (isNaN(h)) {
    return '&infin;'
  }
  return h + ':' + s
}

// 时间转换
export function bigNumberTransform(value) {
  const newValue = ['', '', '']
  let fr = 1000
  let num = 3
  let text1 = ''
  let fm = 1
  while (value / fr >= 1) {
    fr *= 10
    num += 1
    // console.log('数字', value / fr, 'num:', num)
  }
  if (num <= 4) {
    // 千
    newValue[0] = parseInt(value / 1000) + ''
    newValue[1] = '千'
  } else if (num <= 8) {
    // 万
    text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
    // tslint:disable-next-line:no-shadowed-variable
    fm = text1 === '万' ? 10000 : 10000000
    if (value % fm === 0) {
      newValue[0] = parseInt(value / fm) + ''
    } else {
      newValue[0] = parseFloat(value / fm).toFixed(0) + ''
    }
    newValue[1] = text1
  } else if (num <= 16) {
    // 亿
    text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
    text1 = (num - 8) / 4 > 1 ? '万亿' : text1
    text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
    // tslint:disable-next-line:no-shadowed-variable
    fm = 1
    if (text1 === '亿') {
      fm = 100000000
    } else if (text1 === '千亿') {
      fm = 100000000000
    } else if (text1 === '万亿') {
      fm = 1000000000000
    } else if (text1 === '千万亿') {
      fm = 1000000000000000
    }
    if (value % fm === 0) {
      newValue[0] = parseInt(value / fm) + ''
    } else {
      newValue[0] = parseFloat(value / fm).toFixed(1) + ''
    }
    newValue[1] = text1
  }
  if (value < 1000) {
    newValue[0] = value + ''
    newValue[1] = ''
  }
  return newValue.join('')
}

export function random(min, max) {
  return Math.floor(Math.random() * (max - min)) + min
}

// 格式化数字
export function formatNumber(num) {
  if (num === 0) return 0
  if (!num) return
  if (Math.abs(num) > 100000000) {
    return Math.floor(num / 100000000) + '亿'
  } else if (Math.abs(num) > 10000) {
    return Math.floor(num / 10000) + '万'
  } else {
    return Math.floor(num)
  }
}
// 格式化视频/音乐等长度
const formatDuration = function (ms) {
  let res = ''
  let h = Math.floor(ms / 3600000) % 24
  let m = Math.floor(ms / 60000) % 60
  let s = Math.floor(ms / 1000) % 60
  if (h > 0) {
    h = h < 10 ? '0' + h : h
    res += `${h}:`
  }
  m = m < 10 ? '0' + m : m
  s = s < 10 ? '0' + s : s
  res += `${m}:${s}`
  return res
}

// 格式化日期
export function formatDate(value, type) {
  const date = new Date(value)
  const y = date.getFullYear()
  let MM = date.getMonth() + 1
  MM = MM < 10 ? '0' + MM : MM
  let d = date.getDate()
  d = d < 10 ? '0' + d : d
  let h = date.getHours()
  h = h < 10 ? '0' + h : h
  let m = date.getMinutes()
  m = m < 10 ? '0' + m : m
  let s = date.getSeconds()
  s = s < 10 ? '0' + s : s
  if (type === 1) {
    return `${MM}月${d}日 ${h}:${m}`
  } else if (type === 2) {
    return `${y}-${MM}-${d}`
  } else if (type === 3) {
    return `${y}-${MM}-${d} ${MM}月${d}日`
  } else if (type === 4) {
    return `${y}年${MM}月${d}日`
  } else if (type === 5) {
    return y
  } else if (type === 6) {
    return `${y}-${MM}-${d} ${MM}月${d}日 ${h}:${m}`
  }
}

const formatCurrentTime = function (t) {
  if (!t) return '00:00'
  let interval = Math.floor(t)
  let minute = Math.floor(interval / 60)
  let second = interval % 60
  let m = minute < 10 ? '0' + minute : minute
  let s = second < 10 ? '0' + second : second
  return `${m}:${s}`
}
